<?php
defined('IN_TS') or die('Access Denied.');
$userid = aac('user')->isLogin();

$price = sprintf("%01.2f",$_POST['price']);

if($price<1){
    tsNotice('充值金额不能少于1元！','点击返回重新输入！',tsUrl('pay'));
}

$types = trim($_POST['types']);
$forpayid = intval($_GET['forpayid']);

if($forpayid){
	//已有单号
	$strPayUser = $new['pay']->find('pay_user',array('forpayid'=>$forpayid));
	if($strPayUser['status'] == 1) tsNotice('该单已支付过！','返回>>',tsUrl('pay'));
	$payid = $strPayUser['payid'];
	$price = $strPayUser['price'];
	$types = trim($strPayUser['types']);
}else{
	//生成订单
	$arrData = array(
		'userid'=>$userid,
		'types'=>$types,
		'price'=>$price,
		'addtime'=>time(),
	);
	$payid = $new['pay']->create('pay_user',$arrData);
	$forpayid = $payid + $arrData['addtime'];
	$new['pay']->update('pay_user',array(
		'payid'=>$payid
	),array(
		'forpayid'=>$forpayid
	));
}
	
	
	
/*
 * 支付宝支付
 */
if($types=='alipay'){
    require_once(THINKROOT."/create_direct_pay_by_user-PHP-UTF-8/alipay.config.php");
    require_once(THINKROOT."/create_direct_pay_by_user-PHP-UTF-8/lib/alipay_submit.class.php");

    /**************************请求参数**************************/

    //支付类型
    $payment_type = "1";
    //必填，不能修改
    //服务器异步通知页面路径
    $notify_url = $alipay_config['siteurl']."/create_direct_pay_by_user-PHP-UTF-8/notify_url.php";
    //需http://格式的完整路径，不能加?id=123这类自定义参数

    //页面跳转同步通知页面路径
    $return_url = $alipay_config['siteurl']."/create_direct_pay_by_user-PHP-UTF-8/return_url.php";
    //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/

    //卖家支付宝帐户
    //$seller_email = $_POST['WIDseller_email'];
    $seller_email = $alipay_config['seller_email'];
    //必填

    //商户订单号
    //$out_trade_no = $_POST['WIDout_trade_no'];
    $out_trade_no = $forpayid;
    //商户网站订单系统中唯一订单号，必填

    //订单名称
    //$subject = $_POST['WIDsubject'];
    $subject = $_SERVER['HTTP_HOST'].$forpayid.'userid'.$userid.'price'.$price;
    //必填

    //付款金额
    //$total_fee = $_POST['WIDtotal_fee'];
    $total_fee = $price;
    //必填

    //订单描述

    //$body = $_POST['WIDbody'];
    $body = $_SERVER['HTTP_HOST'].$forpayid.'userid'.$userid.'price'.$price.$arrData['addtime'];
    //商品展示地址
    //$show_url = $_POST['WIDshow_url'];
    $show_url = $alipay_config['siteurl'];
    //需以http://开头的完整路径，例如：http://www.商户网址.com/myorder.html

    //防钓鱼时间戳
    $anti_phishing_key = "";
    //若要使用请调用类文件submit中的query_timestamp函数

    //客户端的IP地址
    $exter_invoke_ip = "";
    //非局域网的外网IP地址，如：221.0.0.1


    /************************************************************/

    //构造要请求的参数数组，无需改动
    $parameter = array(
        "service" => "create_direct_pay_by_user",
        "partner" => trim($alipay_config['partner']),
        "payment_type"	=> $payment_type,
        "notify_url"	=> $notify_url,
        "return_url"	=> $return_url,
        "seller_email"	=> $seller_email,
        "out_trade_no"	=> $out_trade_no,
        "subject"	=> $subject,
        "total_fee"	=> $total_fee,
        "body"	=> $body,
        "show_url"	=> $show_url,
        "anti_phishing_key"	=> $anti_phishing_key,
        "exter_invoke_ip"	=> $exter_invoke_ip,
        "_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
    );

    //建立请求
    $alipaySubmit = new AlipaySubmit($alipay_config);
    $html_text = $alipaySubmit->buildRequestForm($parameter,"get", "跳转支付宝中...");
    echo $html_text;
}




/*
 * 微信支付
 */
if($types=='weixin'){

    $price2 = str_replace('.','',$price);
    $price2 = $price2*100;

    require_once "WxpayAPI_php_v3/lib/WxPay.Api.php";
    require_once "WxpayAPI_php_v3/example/WxPay.NativePay.php";
    require_once 'WxpayAPI_php_v3/example/log.php';
    $notify = new NativePay();
    $input = new WxPayUnifiedOrder();
    $input->SetBody($_SERVER['HTTP_HOST'].$forpayid.'userid'.$userid.'price'.$price);
    $input->SetAttach($forpayid);
    $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
    $input->SetTotal_fee($price2);
    $input->SetTime_start(date("YmdHis"));
    $input->SetTime_expire(date("YmdHis", time() + 6000));
    $input->SetGoods_tag("test");
    $input->SetNotify_url(SITE_URL."WxpayAPI_php_v3/example/notify.php");
    $input->SetTrade_type("NATIVE");
    $input->SetProduct_id($forpayid);
    $result = $notify->GetPayUrl($input);

    //print_r($result);

    $url2 = $result["code_url"];


    $title = '微信支付';
    include template('weixin');

}

